package com.example.demo;

import com.example.demo.bean.RespBean;
import com.example.demo.bean.User;
import com.example.demo.mapper.MenuMapper;
import com.example.demo.mapper.SalesMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mytest.StoreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello jie 好帅fsf";
    }

    @Resource
    private UserMapper userMapper;
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private SalesMapper salesMapper;

    @GetMapping("/user")
    public Map<String, Object> queryAllUser(@RequestParam(defaultValue = "1") Integer page, String keywords,@RequestParam(defaultValue = "2") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, pageSize);
        List<User> allUser = userMapper.getAllUser("%" + keywords + "%");
        PageInfo<User> users = new PageInfo<>(allUser);
        map.put("count", users.getTotal());
        map.put("user", users.getList());
        return map;
    }

    @PostMapping("/addUser")
    public RespBean addUser(User user) {
        int result = userMapper.addUser(user);
        if (result > 0) {
            return RespBean.ok("添加成功");
        } else {
            return RespBean.error("添加失败");
        }
    }

    @DeleteMapping("/delUser/{id}")
    public RespBean delUser(@PathVariable String id) {
        int result = userMapper.delUser(id);
        if (result > 0) {
            return RespBean.ok("删除成功");
        } else {
            return RespBean.error("删除失败");
        }
    }

    @PutMapping("/updateUser")
    public RespBean updateUser(User user) {
        int result = userMapper.updateUser(user);
        if (result > 0) {
            return RespBean.ok("修改成功");
        } else {
            return RespBean.error("修改失败");
        }
    }

    /**
     * 用于查询TableTree
     */
    @GetMapping("/allMenu")
    public List allMenu() {
        return menuMapper.getAllMenu();
    }

    @Resource
    StoreService storeService;
    @GetMapping("/aaa")
    public void aa(){
        storeService.sayHi();
    }

    /**
     * 用于前台的报表查询
     * @return
     */
    @GetMapping("/sales")
    public List sales(){
        return salesMapper.getAllSales();
    }
}

